package com.hzdl.cinema.web.servlet;

import com.hzdl.cinema.Service.impl.UserServiceImpl;
import com.hzdl.cinema.domain.Msg;
import com.hzdl.cinema.domain.User;
import com.hzdl.cinema.Service.UserService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    UserService userService = new UserServiceImpl();

    public void findUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = new User();
        user.setUserName("aaa");
        user.setUserId(2);
        user.setUserPassword("aaa111");
        user.setUserEmail("aaa@aa.com");
        request.getSession().setAttribute("userLogin", user);
        User userLogin = (User) request.getSession().getAttribute("userLogin");
        writeValue(response, userLogin);
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Msg msg = new Msg();
        HttpSession session = request.getSession();
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        User userLogin = userService.login(user);
        int flag = 1;
        if (userLogin == null) {
            flag = 2;
        }
        if (flag == 2) {
            msg.setCode(200);
            msg.setMsg("用户名或密码错误");
            writeValue(response, msg);
            return;
        }

        session.setAttribute("userLogin", userLogin);
        if (flag == 1) {
            msg.setCode(100);
            msg.add("msg", userLogin.getUserStatus());

            writeValue(response, msg);
            return;
        }


    }

    public void getUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User userLogin = (User) request.getSession().getAttribute("userLogin");
        writeValue(response, userLogin);
    }

    public void exitUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.invalidate();

    }

    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Msg msg = new Msg();
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        boolean flag = userService.register(user);
        if (!flag) {
            //已存在
            msg.setCode(200);
            msg.setMsg("用户名已存在");
            writeValue(response, msg);
            return;
        }
        msg.setCode(100);
        msg.setMsg("注册成功");
        writeValue(response, msg);
        return;
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Msg msg = new Msg();
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        boolean flag = userService.update(user);
        if (flag) {
            msg.setCode(100);
            msg.setMsg("修改成功");
            writeValue(response, msg);
            return;

        }
        msg.setCode(200);
        msg.setMsg("用户名不存在");
        writeValue(response, msg);
        return;
    }

    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<User> userList = userService.findAll();
        writeValue(response,userList);

    }
    public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        User user=userService.findStudioById(userId);
        writeValue(response,user);

    }
    public void updata_user(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        userService.update_users(userId,user);


    }
    public void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(userService,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        boolean flag=userService.addUser(user);
        writeValue(response,flag);

    }
    public void deleteUserById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int user = Integer.parseInt(request.getParameter("userId"));
        userService.deleteStudioById(user);

    }

    public void searchUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        userName= new String(userName.getBytes("ISO8859_1"), "utf-8");
        User studio=userService.searchUserByName(userName);
        writeValue(response,userName);


    }


}
